package cn.famouscat.ex003;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Scroller;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huangjin.
 * 2016/01/22
 */
public class MyView extends View {

    private int[] mColors = {
            0xFFCCFF00, 0xFF6495ED,
            0xFFE32636, 0xFF800000,
            0xFF808000, 0xFFFF8C69,
            0xFF808080, 0xFFE6B800,
            0xFF7CFC00};

    private Scroller mScroller;

    private Context mContext;

    private List<Pie> pieList;

    private Paint mPaint;

    private float mWidth;
    private float mHeight;

    private float mR;
    private RectF mRectF;

    int flag = 0;

    public MyView(Context context) {
        super(context);
        init(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        this.mContext = context;
        mScroller = new Scroller(mContext);
        mPaint = new Paint();

        mPaint.setAntiAlias(true);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width = 0;
        int height = 0;

        switch (widthMode){
            case MeasureSpec.EXACTLY:{
                width = widthSize;
            }break;
            case MeasureSpec.AT_MOST:{
                width = 400;
            }break;
        }

        switch (heightMode){
            case MeasureSpec.EXACTLY:{
                height = heightSize;
            }break;
            case MeasureSpec.AT_MOST:{
                height = 400;
            }break;
            default:height = 400;
        }

        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(pieList == null){
            return;
        }
        canvas.translate(mWidth/2,mHeight/2);

        mR = (Math.min(mWidth,mHeight)/2)*0.8f;
        mRectF = new RectF(-mR,-mR,mR,mR);

        int size = pieList.size();
        float sumAngle = 0f;
        for(int i = 0;i<size;i++){
            mPaint.setColor(pieList.get(i).fillColor);
            canvas.drawArc(mRectF,sumAngle,pieList.get(i).angle,true,mPaint);
            sumAngle += pieList.get(i).angle;
        }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    public void setData(int[]  data){
        if(data == null || data.length == 0){
            return;
        }
        pieList = new ArrayList<>();
        int sum = 0;
        for(int i=0;i<data.length;i++){
            if(data[i]<=0){
                continue;
            }
            sum = sum + data[i];
            Pie p = new Pie();
            int j = i % mColors.length;       //设置颜色
            p.fillColor = mColors[j];
            pieList.add(p);

        }

        for(int i=0;i<data.length;i++){
            if(data[i]<=0){
                continue;
            }
            pieList.get(i).angle = 360.0f/sum*data[i];
        }

        invalidate();
    }

    private class Pie{
        public float angle;
        public int fillColor;
    }


    @Override
    public void computeScroll() {
        Log.d("tag","====computeScroll");

        super.computeScroll();
        if(mScroller.computeScrollOffset()){
            Log.d("tag","=====x:"+mScroller.getCurrX()+";;;y:"+mScroller.getCurrY());
            scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
            invalidate();
        }
    }

    public void smoothScrollTo(int destX,int destY){
        int currX = getScrollX();
        int currY = getScrollY();

        Log.d("tag","====currX:"+currX+";;;currY:"+currY);
        int deltaX = destX - currX;
        int deltaY = destY - currY;

        Log.d("tag","====deltaX:"+deltaX+";;;deltaY:"+deltaY);
        mScroller.startScroll(currX,currY,deltaX,deltaY,1000);
        invalidate();
    }


}
